<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>首页</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="" />

<!-- easyui -->
<script type="text/javascript" th:src="@{/easyui/jquery.min.js}" ></script>
<script type="text/javascript" th:src="@{/easyui/jquery.easyui.min.js}"></script>
<link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/default/easyui.css}">
<link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/icon.css}">
<link rel="stylesheet" type="text/css" th:href="@{/easyui/themes/color.css}">
	<script type="text/javascript" th:src="@{/easyui/locale/easyui-lang-zh_CN.js}"></script>
</head>
<body>
	<!-- 表格&工具条 -->
	<div th:replace="commons/pub::#list"></div>
	<div th:replace="commons/pub::#tool_bar"></div>
	<!-- 编辑窗口 -->
	<div id="win">
		<form id="editForm" method="post">
			教师账号：<input name="tid" type="number" class="easyui-validatebox" data-options="required:true"/><br>
			姓名：<input id="tname" name="tname" class="easyui-validatebox" data-options="required:true"/><br>
			<!--<div class="pwd">密码：<input name="password" class="easyui-validatebox" /><br></div>-->
			性别：<input type="radio" name="gender" class="easyui-validatebox" value="男"/>男
			<input type="radio" name="gender" class="easyui-validatebox" value="女"/>女<br>
			学院:<input name="college" class="easyui-validatebox" data-options="required:true"/><br>
			电话:<input name="telephone" class="easyui-validatebox" data-options="required:true"/><br>
			状态:<input name="state" type="radio" class="easyui-validatebox" value="1"/>正常
			<input name="state" type="radio" class="easyui-validatebox"  value="0"/>停用<br>
			<a id="saveBtn" href="#" class="easyui-linkbutton">保存</a>
		</form>
	</div>
	<script type="text/javascript" th:inline="none">
	$(function(){
		$('#list').datagrid({
		    url:'/teacher/listByPage',
		    columns:[[   
		        {field:'id',title:'编号',checkbox:true},
		        {field:'tid',title:'教师账号',width:'10%',align:'center'},
		        {field:'tname',title:'姓名',width:'10%',align:'center'},
		        {field:'gender',title:'性别',width:'10%',align:'center'},
				{field:'college',title:'学院',width:'13%',align:'center'},
				{field:'telephone',title:'电话',width:'15%',align:'center'},
				{field:'create_time',title:'创建时间',width:'25%',align:'center'},
				{field:'state',title:'状态',width:'15%',align:'center'}
		    ]],
		    pagination:true,
		    toolbar:"#tool_bar"
		});
		$('#win').window({
		    width:600,   
		    height:400, 
		    padding:30,
		    closed:true,
		    title:"用户编辑",
		    modal:true  
		});
		/*增加用户*/
		$("#addBtn").click(function(){
			$("#editForm").form("clear");
			$("#win").window("open");
			$("#tname").after("<div id='pwd'><br>密码：<input name='password' class='easyui-validatebox' data-options='required:true' /></div>");
		});
		/*保存*/
		$("#saveBtn").click(function () {
			$("#editForm").form("submit",{
				url:"/teacher/saveTeacher",     //提交表单
				onSubmit:function(){
					return $("#editForm").form("validate");
				},
				success:function(data){   //回传数据
					data=eval("("+data+")")
					if(data.success){
						$("#win").window("close");
						$("#list").datagrid("reload");
						$.messager.alert("提示","保存成功!","info");
					}else{
						$.messager.alert("提示","保存失败：",data.msg,"error");
					}
				}
			});
		})
		//修改数据
		$("#editBtn").click(function(){
			//判断只能选择一行
			var rows=$("#list").datagrid("getSelections");
			if(rows.length!=1){
				$.messager.alert("提示","修改操作只能选择一行","warning");
				return;
			}
			$("#pwd").remove();
			//表单回显
			$("#editForm").form("load","/teacher/findTeacherById?tid="+rows[0].tid+"");
			$("#win").window("open");
		});
		//删除功能
		$("#deleteBtn").click(function(){
			//判断只能选择一行
			var rows=$("#list").datagrid("getSelections");
			if(rows.length==0){
				$.messager.alert("提示","删除操作至少选择一行","warning");
				return;
			}
			$.messager.confirm("提示","确定删除吗？",function(value){
				if(value){
					//执行删除,遍历数据
					var idStr= "";
					$(rows).each(function(i){
						idStr+=("id="+rows[i].sid);
						if(i!=$(rows).length-1){
							idStr+="&";
						}
					});
					$.post("/teacher/delectTeacher",idStr,function(data){
						if(data.success){
							$("#list").datagrid("reload");
							$.messager.alert("提示","删除成功!","info");
						}else{
							$.messager.alert("提示","删除失败：",data.msg,"error");
						}
					},"json");
				}
			});
		});
	});
	</script>

</body>